<style>
    .panel {
        color: #333;
        padding: 6px;
        border: 1px solid silver;
        box-shadow: 3px 4px 3px 0px silver;
        position: absolute;
        background-color: #eee;
        top: 10px;
        right: 10px;
        border-radius: 5px;
        overflow: hidden;
        line-height: 20px;
    }

    .panel .address {
        width: 250px;
        height: 25px;
    }
</style>
<section class="content">
    <div class="row addsullier">
        <div class="col-md-12">
            <div class="box box-danger">
                <div class="box-body ">
                    <div id="container3" style="width: 100%;height: 400px;"></div>
                    <div id="status">
                        <div style="color: red;margin-top: 5px;"><span id="roads"></span></div>

                    </div>
                    <div style="clear: both;"></div>
                    <if condition='$type eq 0'>
                        <div class="box-footer">
                               <span class="input-group-btn">
                                    <button class="btn btn-primary pull-right" type="button" id='form_submit2'>提交</button>
                                </span>
                        </div>
                   </if>
                </div>
            </div>
        </div>
    </div>
    <link href="http://api.map.baidu.com/library/TrafficControl/1.4/src/TrafficControl_min.css" rel="stylesheet"
          type="text/css"/>
    <script type="text/javascript"
            src="http://api.map.baidu.com/library/TrafficControl/1.4/src/TrafficControl_min.js"></script>
    <if condition='$type eq 0'>
    <script type="text/javascript">
        var icon, marker, geocoder, infoWindow;
        var slng = '{$slng}', slat = '{$slat}', elng = '{$elng}', elat = '{$elat}';
        var distance = "{$distance}";
        var duration = "{$duration}";
        var point = "";
        $(function () {
            setTimeout(function () {
                var centerPoint = new BMap.Point(112.97935279, 28.21447823);  // 创建点坐标
                map = new BMap.Map("container3");

                map.enableScrollWheelZoom();
                map.centerAndZoom(centerPoint, 12);
                var ctrl = new BMapLib.TrafficControl({
                    showPanel: false //是否显示路况提示面板
                });

                map.addControl(ctrl);
                ctrl.show();
                ctrl.setAnchor(BMAP_ANCHOR_BOTTOM_RIGHT);

                var transit = new BMap.DrivingRoute(map, {
                    renderOptions: {
                        map: map,
                        enableDragging: true //可进行拖拽
                    },
                });
                var dbpoint = "{$point}";
                var p1 = new BMap.Point(slng, slat);
                var p2 = new BMap.Point(elng, elat);
                if(dbpoint==""){
                    transit.search(p1, p2);
                }else{
                    var points = dbpoint.split("|");
                    var pa = [];
                    for(var i=0;i<points.length;i++){
                        var p = points[i];
                        var plnglat = p.split(",");
                        pa[pa.length] = new BMap.Point(plnglat[0], plnglat[1]);
                    }
                    transit.search(p1, p2,{waypoints:pa});
                }

                transit.setSearchCompleteCallback(function (results) {
                    if (transit.getStatus() != BMAP_STATUS_SUCCESS) {
                        return;
                    }

                    var plan = results.getPlan(0);
                    if(plan.getDragPois().length>3){
                        transit.clearResults();
                        alert("只能添加3个途径点");
                        var localResultPoi = plan.getDragPois();
                        point = "";
                        var pa = [];
                        for (var i = 0; i < 3; i++) {
                            var poi = localResultPoi[i];
                            point += poi.point.lng + "," + poi.point.lat +"," + poi.title + "|";
                            pa[pa.length] = poi;
                        }
                        transit.search(p1, p2,{waypoints:pa});
                        return;
                    }
                    var output = "计算总路程为：";
                    output += plan.getDistance(true) + "\n";             //获取距离
                    output += " 用时：";
                    output += plan.getDuration(true) + "\n";                //获取时间
                    $("#roads").html(output);
                    distance = plan.getDistance(false);//返回数值（单位为米）
                    duration = plan.getDuration(false);//仅返回数值（单位为秒）
                    var localResultPoi = plan.getDragPois();
                    point = "";
                    for (var i = 0; i < localResultPoi.length; i++) {
                        var poi = localResultPoi[i];
                        point += poi.point.lng + "," + poi.point.lat +"," + poi.title + "|";
                    }
                    //上传编辑线路
//                    $("#roads").html(point);
                });
            }, 200);
        });


        $('#form_submit2').click(function () {
            $.post("{:U('Task/setway')}","point="+point+"&id={$id}&distance="+distance+"&duration="+duration, function (res) {
                    if (!res.status) {
                        $.messager.alert('提示信息', res.info, 'error');
                    } else {
                        $.messager.alert('提示信息', res.info, 'info');
                        dialog.get('getRoads').close().remove();
                        taskTable.fnDraw(false);
                    }
                });
            return false;

        });


    </script>
    <else/>
        <script type="text/javascript">
            var icon, marker, geocoder, infoWindow;
            var slng = '{$slng}', slat = '{$slat}', elng = '{$elng}', elat = '{$elat}';
            var distance = "{$distance}";
            var duration = "{$duration}";
            var point = "";
            $(function () {
                setTimeout(function () {
                    var centerPoint = new BMap.Point(112.97935279, 28.21447823);  // 创建点坐标
                    map = new BMap.Map("container3");

                    map.enableScrollWheelZoom();
                    map.centerAndZoom(centerPoint, 12);
                    var ctrl = new BMapLib.TrafficControl({
                        showPanel: false //是否显示路况提示面板
                    });

                    map.addControl(ctrl);
                    ctrl.show();
                    ctrl.setAnchor(BMAP_ANCHOR_BOTTOM_RIGHT);

                    var transit = new BMap.DrivingRoute(map, {
                        renderOptions: {
                            map: map,
                            enableDragging: false //可进行拖拽
                        },
                    });
                    var dbpoint = "{$point}";
                    var p1 = new BMap.Point(slng, slat);
                    var p2 = new BMap.Point(elng, elat);
                    if(dbpoint==""){
                        transit.search(p1, p2);
                    }else{
                        var points = dbpoint.split("|");
                        var pa = [];
                        for(var i=0;i<points.length;i++){
                            var p = points[i];
                            var plnglat = p.split(",");
                            pa[pa.length] = new BMap.Point(plnglat[0], plnglat[1]);
                        }
                        transit.search(p1, p2,{waypoints:pa});
                    }
                    transit.setSearchCompleteCallback(function (results) {
                        if (transit.getStatus() != BMAP_STATUS_SUCCESS) {
                            return;
                        }

                        var plan = results.getPlan(0);

                        var output = "计算总路程为：";
                        output += plan.getDistance(true) + "\n";             //获取距离
                        output += " 用时：";
                        output += plan.getDuration(true) + "\n";                //获取时间
                        $("#roads").html(output);
                        //上传编辑线路
//                    $("#roads").html(point);
                    });
                }, 200);
            });


        </script>
    </if>
</section>

